package me.tuyou.wordcount;

import me.tuyou.common.MyOrder;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.BatchTableEnvironment;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.$;

public class SQLBatchWordCountTest {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env);
        DataSource<MyOrder> input = env.fromElements(new MyOrder(1L, "tuyou", 1),
                new MyOrder(2L, "yangrui", 8),
                new MyOrder(3L, "tuyou", 3),
                new MyOrder(4L, "yangrui", 5));

        tableEnv.createTemporaryView("MyOrder", input, $("id"), $("product"), $("amount"));
        Table table = tableEnv.sqlQuery("select product, sum(amount) as amount from MyOrder group by product");
        tableEnv.toDataSet(table, Row.class).print();
    }
}
